#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require

#include "../../../../core/shader_std.h"
#include "../../../../core/shader_built_in_functions.h"
#include "../../../../core/shader_mesh.h"


layout(push_constant, scalar) uniform DRAWPROP_push_constants {
	S_ConstPush_Mesh_Line const_push;
};

layout(std140, set = 0, column_major, binding = DEF_BINDING_PROJECT) uniform Buf0 {
	mat4 mat_ProjecView[4];
};

layout(std430, set = 1, scalar, column_major, binding = DEF_binding_L1_3D_Tranform) readonly buffer Buf1 {
	mat4 mat_Model[];
};


layout(location = 0) in vec3 inPos;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec3 inUV_Color;


layout(location = 1) out VS_GS_VERTEX {
	mat4 vProj;
	mat4 vModel;

	vec4 vCubeColor;
	vec3 vCubePos;
	vec3 vCubeSize;
} vertex_out;

//vec3 vs[3] = {vec3(-100,-500,0), vec3(-100,500,0), vec3(500,00,0)};


void removeRotation(inout mat4 transform) {
    // 提取平移分量（假设为列主序矩阵）
    vec3 translation = transform[3].xyz;
    
    // 提取缩放因子（各轴的缩放比例）
    float scaleX = length(transform[0].xyz); // 第一列的长度
    float scaleY = length(transform[1].xyz); // 第二列的长度
    float scaleZ = length(transform[2].xyz); // 第三列的长度
    
    // 构建新的变换矩阵（不含旋转）
    transform = mat4(
        scaleX, 0.0, 0.0, 0.0,
        0.0, scaleY, 0.0, 0.0,
        0.0, 0.0, scaleZ, 0.0,
        translation.x, translation.y, translation.z, 1.0
    );
}

void main(void) {
	mat4 bonesMat_som = mat_Model[const_push.m_Offset_Tranform];
	mat4 mxx = mat4(1);
	mxx[3].xyz = bonesMat_som[3].xyz;
	//removeRotation(bonesMat_som);

	vertex_out.vProj = mat_ProjecView[DEF_Camera_Matrix_PV] * mxx;
	vertex_out.vModel = mxx;
	vertex_out.vCubePos = inPos;
	vertex_out.vCubeSize = inNormal;
	//gl_PointSize = 3.0;
	
	if (const_push.m_Ob_ShaderType == DEF_3DShaderType_PerObColor) {
		vertex_out.vCubeColor = f_glsl_buildColor(const_push.m_Ob_Color);
	} else {
		vertex_out.vCubeColor = f_glsl_buildColor(floatBitsToUint(inUV_Color.z));
	}

	//vec4 ndcpos = gl_Position / gl_Position.w;
	//ndcpos.xy += mat_ProjecView[DEF_Camera_Matrix_Porj][0].xy;
	//gl_Position = ndcpos * gl_Position.w;
}

